package com.yt.sys.server.api.cms;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yt.common.constant.Result;
import com.yt.sys.models.dto.SysRoleDTO;
import com.yt.sys.models.entity.SysRole;
import com.yt.sys.models.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/cms/sys/role")
@Api(tags = "系统管理-角色管理")
public class SysRoleController {

    @Autowired
    private ISysRoleService sysRoleService;

    @GetMapping("/list")
    @ApiOperation("角色列表")
    public Result<List<SysRole>> list() {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("create_time");
        List<SysRole> list = sysRoleService.list(queryWrapper);
        return Result.success(list);
    }

    @GetMapping("/page")
    @ApiOperation("角色分页列表")
    public Result<Page<SysRole>> page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String roleName,
            @RequestParam(required = false) String roleDesc) {
        Page<SysRole> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        
        // 添加搜索条件
        if (roleName != null && !roleName.isEmpty()) {
            queryWrapper.like("role_name", roleName);
        }
        if (roleDesc != null && !roleDesc.isEmpty()) {
            queryWrapper.like("role_desc", roleDesc);
        }
        
        // 移除手动添加的 is_delete 条件，因为 MyBatis-Plus 会自动处理逻辑删除字段
        queryWrapper.orderByDesc("create_time");
        Page<SysRole> result = sysRoleService.page(page, queryWrapper);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID获取角色信息")
    public Result<SysRole> getById(@PathVariable Integer id) {
        SysRole role = sysRoleService.getById(id);
        if (role == null || role.getIsDelete() == 1) {
            return Result.fail("角色不存在");
        }
        return Result.success(role);
    }

    @PostMapping
    @ApiOperation("新增角色")
    public Result<String> add(@Valid @RequestBody SysRoleDTO sysRoleDTO) {
        // 检查角色名称是否已存在
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_name", sysRoleDTO.getRoleName());
        queryWrapper.eq("is_delete", 0);
        if (sysRoleService.count(queryWrapper) > 0) {
            return Result.fail("角色名称已存在");
        }

        SysRole role = new SysRole();
        BeanUtils.copyProperties(sysRoleDTO, role);
        role.setIsDelete(0);
        boolean result = sysRoleService.save(role);
        if (result) {
            return Result.success("新增成功");
        } else {
            return Result.fail("新增失败");
        }
    }

    @PutMapping
    @ApiOperation("修改角色")
    public Result<String> update(@Valid @RequestBody SysRoleDTO sysRoleDTO) {
        if (sysRoleDTO.getRoleId() == null) {
            return Result.fail("角色ID不能为空");
        }

        SysRole role = sysRoleService.getById(sysRoleDTO.getRoleId());
        if (role == null || role.getIsDelete() == 1) {
            return Result.fail("角色不存在");
        }

        // 检查角色名称是否被其他人使用
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_name", sysRoleDTO.getRoleName());
        queryWrapper.eq("is_delete", 0);
        queryWrapper.ne("role_id", sysRoleDTO.getRoleId());
        if (sysRoleService.count(queryWrapper) > 0) {
            return Result.fail("角色名称已存在");
        }

        BeanUtils.copyProperties(sysRoleDTO, role);
        boolean result = sysRoleService.updateById(role);
        if (result) {
            return Result.success("修改成功");
        } else {
            return Result.fail("修改失败");
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除角色")
    public Result<String> delete(@PathVariable Integer id) {
        SysRole role = sysRoleService.getById(id);
        if (role == null || role.getIsDelete() == 1) {
            return Result.fail("角色不存在");
        }

        // 使用 MyBatis-Plus 的逻辑删除功能
        boolean result = sysRoleService.removeById(id);
        if (result) {
            return Result.success("删除成功");
        } else {
            return Result.fail("删除失败");
        }
    }
}